package com.xingdata.jjxc.adapter;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xingdata.jjxc.R;
import com.xingdata.jjxc.enty.CarcorderEntity;
import com.xingdata.jjxc.utils.BitmapCache;
import com.xingdata.jjxc.utils.BitmapThumbnailTask;
import com.xingdata.jjxc.utils.BitmapThread;
import com.xingdata.jjxc.utils.Consumer;
import com.xingdata.jjxc.utils.ImageTools;
import com.xingdata.jjxc.utils.MessageCode;
import com.xingdata.jjxc.utils.SDCardTools;
import com.xingdata.jjxc.utils.StringUtil;
import com.xingdata.jjxc.utils.VideoTools;
import com.xingdata.jjxc.views.MyImageView;

public class HudVideoAdapter extends BaseAdapter {
	private String TAG = "HudVideoAdapter";
	private Context mContext;
	private ArrayList<CarcorderEntity> carcorderEntities;
	private LayoutInflater inflater;
	// private HashMap<String, Bitmap> bitMap = new HashMap<String, Bitmap>();
	private LruCache<String, Bitmap> bitMap = new LruCache<String, Bitmap>(
			1 * 1024 * 1024);
	private Handler handler;
	private Object synchronizedObject = new Object();
	private ArrayList<ImageView> imageViews;
	private ArrayList<String> downLoadURLs = new ArrayList<String>();
	private Consumer consumer;

	public HudVideoAdapter(Context mContext, ArrayList<CarcorderEntity> list,
			ArrayList<ImageView> imageViews, Consumer c) {
		this.mContext = mContext;
		this.carcorderEntities = list;
		this.imageViews = imageViews;
		this.consumer = c;
		inflater = LayoutInflater.from(mContext);
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MessageCode.MessageCode_RefreshControl:
					notifyDataSetChanged();
					break;

				default:
					break;
				}
			};
		};
	}

	@Override
	public int getCount() {
		return carcorderEntities == null ? 0 : carcorderEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return carcorderEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.circulation, null);
			viewHolder = new ViewHolder();
			// convertView.findViewById(R.id.title);
			viewHolder.videoImage = (MyImageView) convertView
					.findViewById(R.id.hud_video_image);
			LayoutParams params = viewHolder.videoImage.getLayoutParams();
			params.height = (720 * 9 - 360) / 32;
			viewHolder.videoImage.setLayoutParams(params);
			((LinearLayout) convertView.findViewById(R.id.video_layout))
					.setLayoutParams(params);
			viewHolder.crtTime = (TextView) convertView
					.findViewById(R.id.video_playtime);
//			viewHolder.videoSize = (TextView) convertView
//					.findViewById(R.id.video_size);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (!imageViews.contains(viewHolder.videoImage)) {
			imageViews.add(viewHolder.videoImage);
		}
		// viewHolder.title.setText(pictures.get(position).getTitle());
		// viewHolder.image.setImageResource(pictures.get(position).getImageId());
		String imageURL = ImageTools.changeImageName(
				carcorderEntities.get(position).getHud_url()).replaceAll(
				".mp4", ".jpg");
		if (BitmapCache.get(carcorderEntities.get(position).getHud_url()) != null) {
			viewHolder.videoImage.setImageBitmap(BitmapCache
					.get(carcorderEntities.get(position).getHud_url()));
		} else {
			// if (!downLoadURLs.contains(carcorderEntities.get(position)
			// .getHud_url())) {
			// new getBitMapThread(carcorderEntities.get(position)
			// .getHud_url()).start();
//			downLoadURLs.add(carcorderEntities.get(position).getHud_url());
			// new BitmapThread(carcorderEntities.get(position).getHud_url(),
			// handler,bitMap).start();

			if (consumer == null) {
				consumer = new Consumer();
			}
			if (!downLoadURLs.contains(carcorderEntities.get(position)
					.getHud_url())) {
				downLoadURLs.add(carcorderEntities.get(position)
						.getHud_url());
				consumer.add(new BitmapThumbnailTask(carcorderEntities.get(position)
						.getHud_url(), handler,4.0f));
			}

		}
		viewHolder.crtTime.setText(StringUtil.ToStringData(new Date(Long
				.parseLong(carcorderEntities.get(position).getfTime()))));
//		viewHolder.videoSize.setText(StringUtil.formatSize(carcorderEntities
//				.get(position).getfSize()));
		return convertView;
	}

	class ViewHolder {
		// 创建时间
		private TextView crtTime;
		// 视频大小
		private TextView videoSize;
		// 视频缩略图
		private MyImageView videoImage;
	}
//
//	class getBitMapThread extends Thread {
//		String url;
//
//		public getBitMapThread(String url) {
//			this.url = url;
//			downLoadURLs.add(url);
//		}
//
//		@Override
//		public void run() {
//			synchronized (synchronizedObject) {
//				Log.i(TAG, "正在获取缩略图" + url);
//				String imageURL = url.replaceAll(".mp4", ".jpg");
//				Bitmap bitmap = SDCardTools.getSDImage(imageURL);
//				if (bitmap == null) {
//					bitmap = VideoTools.createNetVideoThumbnail(url);
//					SDCardTools.SaveUrlImage(imageURL, bitmap);
//				}
//				bitMap.put(ImageTools.changeImageName(imageURL), bitmap);
//				Message message = new Message();
//				message.what = MessageCode.MessageCode_RefreshControl;
//				handler.sendMessage(message);
//			}
//			super.run();
//
//		}
//	}
}
